import Vue from "vue";
import VueRouter from "vue-router";
import AdminHome from "../views/AdminHome.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/index",
  },
  {
    path: "/PetMedical",
    name: "Pet Medical",
    component: () => import("../views/Pet Medical.vue"),
  },
  {
    path: "/wikipedia",
    name: "百科",
    component: () => import("../views/Wikipedia.vue"),
  },
  {
    path: "/index",
    name: "首页",
    component: () => import("../views/Index.vue"),
  },
  {
    path: "/login",
    name: "登录",
    component: () => import("../views/Login.vue"),
    meta: {
      hideLogin: true,
    },
  },
  {
    path: "/register",
    name: "注册",
    component: () => import("../views/Register.vue"),
  },
  {
    path: "/admin",
    redirect: "/admin/login",
  },
  {
    path: "/admin/login",
    name: "管理员登录",
    component: () => import("../views/AdminLogin.vue"),
    meta: {
      requireAuth: true,
    },
  },
  {
    path: "/article",
    name: "article",
    component: () => import("../views/Article.vue"),
  },
  {
    path: "/baike",
    name: "baike",
    component: () => import("../views/Baike.vue"),
  },
  {
    path: "/nav",
    name: "nav",
    component: () => import("../views/Nav.vue"),
  },
  {
    path: "/service",
    name: "service",
    component: () => import("../views/Service.vue"),
  },
  {
    path: "/admin/index",
    name: "index",
    redirect: "/admin/index/home",
    component: () => import("../views/AdminIndex.vue"),
    children: [
      {
        path: "home",
        component: () => import("../views/AdminHome.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "404",
        component: () => import("../views/404.vue"),
      },
      {
        path: "prolist",
        component: () => import("../views/AdminProductList.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "prolist/addpro",
        component: () => import("../views/AdminAddPro.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "proclass",
        component: () => import("../views/AdminProductClass.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "advertising",
        component: () => import("../views/AdminAdvertising.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "advertising/add",
        component: () => import("../views/AdminAdvertisingAdd.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "advclass",
        component: () => import("../views/AdminAdvertisingClass.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "deal",
        component: () => import("../views/AdminDeal.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "dealorder",
        component: () => import("../views/AdminDealOrder.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "dealmoney",
        component: () => import("../views/AdminDealMoney.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "order",
        component: () => import("../views/AdminOrder.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "trading",
        component: () => import("../views/AdminTrading.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "refund",
        component: () => import("../views/AdminRefund.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "account",
        component: () => import("../views/AdminAccount.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "member",
        component: () => import("../views/AdminMember.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "physician",
        component: () => import("../views/AdminPhysician.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "beautician",
        component: () => import("../views/AdminBeautician.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "articlelist",
        component: () => import("../views/AdminArticleList.vue"),
        meta: {
          requireAuth: true,
        },
      },
      {
        path: "article/add",
        component: () => import("../views/AdminArticleAdd.vue"),
        meta: {
          requireAuth: true,
        },
      },
    ],
  },
  {
    path: "/shopcar",
    name: "购物车",
    component: () => import("../views/ShopCar.vue"),
  },
  {
    path: "/details",
    name: "详情页",
    component: () => import("../views/details.vue"),
  },
  {
    path: "/store",
    name: "萌宠商城",
    component: () => import("../views/IndStore.vue"),
    // 路由重定向
    redirect: "/store/dog",
    children: [
      {
        path: "dog",
        component: () => import("../views/dog.vue"),
      },
      {
        path: "cat",
        component: () => import("../views/cat.vue"),
      },
      {
        path: "toy",
        component: () => import("../views/toy.vue"),
      },
    ],
  },
  {
    path: "/home",
    name: "home",
    component: AdminHome,
  },
  {
    path: "/center",
    name: "个人中心",
    component: () => import("../views/UserCenter.vue"),
    redirect: "/center/user",
    meta: {
      requireUserAuth: true,
    },
    children: [
      {
        path: "user",
        component: () => import("../views/UserMessage.vue"),
      },
      {
        path: "pwd",
        component: () => import("../views/Password.vue"),
      },
      {
        path: "phone",
        component: () => import("../views/Phone.vue"),
      },
      {
        path: "mypicture",
        component: () => import("../views/Mypicture.vue"),
      },
      {
        path: "mydeserve",
        component: () => import("../views/Mydeserve.vue"),
      },
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

//路由判断登录，根据路由配置文件的参数
router.beforeEach((to, from, next) => {
  // 管理员权限
  //record.meta.requireAuth是获取到该请求中携带的该参数
  if (to.matched.some((record) => record.meta.requireAuth)) {
    //获取到本地的adminname
    const adminname = sessionStorage.getItem("adminname");
    if (adminname) {
      //如果指向的是登录页面，直接跳到管理员主页面
      if (to.path !== "/admin/login") {
        //如果不是登录页面，且adminname存在，就放行
        next();
      } else {
        next({ path: "/admin/index/home" });
      }
    } else {
      if (to.path !== "/admin/login") {
        next({ path: "/admin/login" });
      } else {
        next();
      }
    }
  } else {
    next();
  }

  // 用户权限
  if (to.matched.some((record) => record.meta.requireUserAuth)) {
  }
});

export default router;
